package com.linchuan.remotecontrol;


import eu.rekawek.toxiproxy.Proxy;
import eu.rekawek.toxiproxy.ToxiproxyClient;
import eu.rekawek.toxiproxy.model.Toxic;
import eu.rekawek.toxiproxy.model.ToxicDirection;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import java.io.IOException;
import java.util.List;

@SpringBootApplication
public class RemoteControlApplication {

	public static void main(String[] args) throws IOException {
		ApplicationContext context = SpringApplication.run(RemoteControlApplication.class, args);

		ToxiproxyClient client = new ToxiproxyClient();

        // 创建proxy
		List<Proxy> proxies;
		proxies = client.getProxies();
		if (proxies == null || proxies.size() == 0) {
			client.createProxy("http", "localhost:9999", "localhost:8889");
			proxies = client.getProxies();
		}

		// 打印已有的proxies
		for(Proxy p: proxies) {
			System.out.println(p.getName());
			System.out.println(p.getListen());
			System.out.println(p.getUpstream());
		}


		// 获取代理
		Proxy p = client.getProxy("http");

		// 设置一个toxic
//		p.toxics().latency("latency-toxic", ToxicDirection.DOWNSTREAM, 2000);
		// 删除一个toxic
		Toxic toxic = null;
		try {
			toxic = p.toxics().get("latency-toxic");
		} catch (IOException e) {
			System.out.println("找不到latency-toxic");
		}

		if (toxic != null) {
			try {
				toxic.remove();
			} catch (IOException e) {
				System.out.println("删除失败");
			}
		}




//		RestTemplate template =  (RestTemplate) context.getBean(org.springframework.web.client.RestTemplate.class);
//		System.out.println(template);
//		Object o = template.getForObject("http://127.0.0.1:8474/proxies", Object.class);
//		System.out.println(o);


//		WebClient webClient = WebClient.create("https://www.baidu.com");
//		WebClient.UriSpec<WebClient.RequestBodySpec> request1 =  webClient.method(HttpMethod.GET);

	}

}
